// // Copyright 2019 The Chromium Authors. All rights reserved.
// // Use of this source code is governed by a BSD-style license that can be
// // found in the LICENSE file.
//
// import 'package:flutter/material.dart';
// import 'package:provider/provider.dart';
//
// void main() {
//   runApp(
//     // Provide the model to all widgets within the app. We're using
//     // ChangeNotifierProvider because that's a simple way to rebuild
//     // widgets when a model changes. We could also just use
//     // Provider, but then we would have to listen to Counter ourselves.
//     //
//     // Read Provider's docs to learn about all the available providers.
//     ChangeNotifierProvider(
//       // Initialize the model in the builder. That way, Provider
//       // can own Counter's lifecycle, making sure to call `dispose`
//       // when not needed anymore.
//       create: (context) => Counter(),
//       child: MyApp(),
//     ),
//   );
// }
//
// /// Simplest possible model, with just one field.
// ///
// /// [ChangeNotifier] is a class in `flutter:foundation`. [Counter] does
// /// _not_ depend on Provider.
// class Counter with ChangeNotifier {
//   int value = 0;
//
//   void increment() {
//     value += 1;
//     notifyListeners();
//   }
// }
//
// class MyApp extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     return MaterialApp(
//       title: 'Flutter Demo',
//       theme: ThemeData(
//         primarySwatch: Colors.blue,
//       ),
//       home: MyHomePage(),
//     );
//   }
// }
//
// class MyHomePage extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(
//         title: Text('Flutter Demo Home Page'),
//       ),
//       body: Center(
//         child: Column(
//           mainAxisAlignment: MainAxisAlignment.center,
//           children: <Widget>[
//             Text('You have pushed the button this many times:'),
//             // Consumer looks for an ancestor Provider widget
//             // and retrieves its model (Counter, in this case).
//             // Then it uses that model to build widgets, and will trigger
//             // rebuilds if the model is updated.
//             Consumer<Counter>(
//               builder: (context, counter, child) => Text(
//                 '${counter.value}',
//                 style: Theme.of(context).textTheme.headline4,
//               ),
//             ),
//           ],
//         ),
//       ),
//       floatingActionButton: FloatingActionButton(
//         onPressed: () {
//           // You can access your providers anywhere you have access
//           // to the context. One way is to use Provider.of<Counter>(context).
//           //
//           // The provider package also defines extension methods on context
//           // itself. You can call context.watch<Counter>() in a build method
//           // of any widget to access the current state of Counter, and to ask
//           // Flutter to rebuild your widget anytime Counter changes.
//           //
//           // You can't use context.watch() outside build methods, because that
//           // often leads to subtle bugs. Instead, you should use
//           // context.read<Counter>(), which gets the current state
//           // but doesn't ask Flutter for future rebuilds.
//           //
//           // Since we're in a callback that will be called whenever the user
//           // taps the FloatingActionButton, we are not in the build method here.
//           // We should use context.read().
//           var counter = context.read<Counter>();
//           counter.increment();
//         },
//         tooltip: 'Increment',
//         child: Icon(Icons.add),
//       ),
//     );
//   }
// }


import 'dart:async';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_mvvm/provider/home_page/counter1.dart';
import 'package:flutter_mvvm/provider/home_page/counter2.dart';
import 'package:flutter_mvvm/view/home_page/my_gesture_detector1.dart';
import 'package:provider/provider.dart';

import 'model/student.dart';
// import 'Counter.dart';






// class Counter3 with Stream {
//
//   void test(){
//     Stream s = Stream.value(42);
//   }
// }

// class Count4 extends Future<int> {
//
// }

// Future<int> getConter4() {
//   return Future.delayed(1,4);
// }

void main() {
  runApp(MyApp());

  // runApp( MultiProvider(
  //   providers:[
  //     ChangeNotifierProvider(create: (context) => Counter1()),
  //     ChangeNotifierProxyProvider<Counter1,Counter2>(
  //         create: (context)=>Counter2(),
  //         update: (_, counter1, counter2) {
  //           if (counter2 == null) throw ArgumentError.notNull('counter2');
  //           counter2.counter1 = counter1;
  //           return counter2;
  //         },
  //     ),
  //     StreamProvider(
  //       initialData: 0.1,
  //       create: (context) => controller.stream,
  //     ),
  //     // StreamProvidher(
  //     //   initialData: 0,
  //     //   create: (_) => Stream.value(8),
  //     // ),
  //     FutureProvider(
  //       initialData: Student('jack', 22),
  //       create: (context) => new Future<Student>(()=>Student('tom', 18)),
  //     ),
  //     // FutureProvider.value(
  //     //   initialData: 0,
  //     //   value: Future.value(42),
  //     // ),
  //   ],
  //   child: MyApp(),
  // ));
}

// Stream generateStream(){
//   Stream s = Stream.value(22.2);
//   return s;
// }

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
        // This makes the visual density adapt to the platform that you run
        // the app on. For desktop platforms, the controls will be smaller and
        // closer together (more dense) than on mobile platforms.
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

class SimpleWidget extends StatelessWidget {
  String text = '';

  SimpleWidget(String value) {
    text = value;
    print(text);
  }

  @override
  Widget build(BuildContext context) {
    // var counter2 = context.watch<Counter2>();
    // text = counter2.value2.toString();
    return Text(text,style: Theme.of(context).textTheme.headline4);
  }
}

// class MyGestureDetector1 extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     var counter1 = context.watch<CounterInt>();
//
//     int value1 = context.select((CounterInt counter1) => counter1.value1);
//
//     return GestureDetector(
//       child: Text(
//         '${value1}',
//         style: Theme.of(context).textTheme.headline4,
//       ),
//       onTap: () {
//         // var counter = context.read<Counter>();
//         counter1.increment1();
//       },
//     );
//   }
// }

class MyGestureDetector2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // var counter2 = context.watch<Counter2>();
    // text = counter2.value2.toString();
    var counter2 = context.watch<Counter2>();

    return GestureDetector(
      child: SimpleWidget('text2: ${counter2.value2}'),
      onTap: () {
        // var counter = context.read<Counter>();
        counter2.increment2();
      },
    );
  }
}

class MyFloatingActionButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var counter1 = context.read<CounterInt>();
    var counter2 = context.read<Counter2>();

    return FloatingActionButton(
      onPressed: () {
        // counter1.cancel();
        counter2.cancel();
      },
      tooltip: 'Cancel',
      child: Icon(Icons.cancel),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final controller = StreamController<double>();
    controller.sink.add(0.2);
    controller.sink.add(0.3);

    return MultiProvider(
      providers:[
        ChangeNotifierProvider(create: (context) => CounterInt()),
        ChangeNotifierProxyProvider<CounterInt,Counter2>(
          create: (context)=>Counter2(),
          update: (_, counter1, counter2) {
            if (counter2 == null) throw ArgumentError.notNull('counter2');
            counter2.counter1 = counter1;
            return counter2;
          },
        ),
        StreamProvider(
          initialData: 0.1,
          create: (context) => controller.stream,
        ),
        FutureProvider(
          initialData: Student('jack', 22),
          create: (context) => new Future<Student>(()=>Student('tom', 18)),
        ),
      ],
      child: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo Home Page'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('You have pushed the button this many times:'),
              // Consumer looks for an ancestor Provider widget
              // and retrieves its model (Counter, in this case).
              // Then it uses that model to build widgets, and will trigger
              // rebuilds if the model is updated.
              Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.all(10.0),
                  child: Column(
                    children: [
                      MyGestureDetector1(
                      ),
                      MyGestureDetector2(
                      )
                    ],
                  )
              ),
            ],
          ),
                    ),
      ),
    );
    // var counter1 = context.read<Counter1>();
    // var counter2 = context.read<Counter2>();
    // double value3 = context.watch<double>();
    // print('value3=$value3');
    // Student student4 = context.watch<Student>();
    // print(student4.name);
    // // Stream value4 = context.watch<Stream>();
    // // print('value3=$value4');
    // return Scaffold(
    //   appBar: AppBar(
    //     title: Text('Flutter Demo Home Page'),
    //   ),
    //   body: Center(
    //     child: Column(
    //       mainAxisAlignment: MainAxisAlignment.center,
    //       children: <Widget>[
    //         Text('You have pushed the button this many times:'),
    //         // Consumer looks for an ancestor Provider widget
    //         // and retrieves its model (Counter, in this case).
    //         // Then it uses that model to build widgets, and will trigger
    //         // rebuilds if the model is updated.
    //         Container(
    //             alignment: Alignment.center,
    //             padding: EdgeInsets.all(10.0),
    //             child: Column(
    //               children: [
    //                 MyGestureDetector1(
    //                 ),
    //                 MyGestureDetector2(
    //                 )
    //               ],
    //             )
    //         ),
    //       ],
    //     ),
    //   ),
    //   floatingActionButton: FloatingActionButton(
    //     onPressed: () {
    //       counter1.cancel();
    //       counter2.cancel();
    //     },
    //     tooltip: 'Cancel',
    //     child: Icon(Icons.cancel),
    //   ),
    // );


  }
}
